﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ITM495.OOExamples.Core.BeeHive
{
    public class BeeHiveController : IController
    {
        public Queen Queen { get; set; }

         public void Run(Options options)
        {
            Console.WriteLine("Welcome to the BEEHIVE!!");
            Console.WriteLine("--------------------------------------------------------");
            
            //Initialize the Bees
            List<IWorker> workers = BuildWorkers();
            Queen = new Queen(workers, 19.0);


            var assignWork = true;

            while (assignWork)
            {
                //Get Desired Job
                var selectedJob = GetJob();

                //Get number of shifts
                var numberShifts = GetNumberOfShifts();
                Console.WriteLine("You selected to work job {0} in {1} shifts.", selectedJob, numberShifts);

                //Assign the work
                if (Queen.AssignWork(selectedJob, numberShifts))
                {
                    Console.WriteLine("The job will be done in '{0}' shifts.", numberShifts);
                }
                else
                {
                    Console.WriteLine("No workers are available to do the job '{0}'.", selectedJob);
                }

                //Assign more work?
                Console.Write("Assign more work? (Y/N):");
                var response = Console.ReadLine();

                assignWork = response.ToLower() == "y";
                Console.Clear();
            }


            var work = true;
            while (Queen.ShiftsLeft > 0)
            {
                Console.WriteLine("\n\nWorking One Shift...........................\n");

                var msg = Queen.WorkTheNextShift();

                Console.WriteLine(new string('-', 55));
                Console.WriteLine("\tShift Report:");
                Console.WriteLine(msg);

                Console.WriteLine("Ther are {0} shifts left.", Queen.ShiftsLeft);
                Console.WriteLine(new string('-', 55));
                Console.WriteLine("Press any key to continue....");
                Console.ReadLine();
            }

        }

        private List<IWorker> BuildWorkers()
        {
            return new List<IWorker>
                       {
                           new RoboBee("Robo 1",
                                       new string[] {JobNames.FlowerNectarCollector, JobNames.HoneyManufaturing}),
                           new RoboBee("Robo 2", new string[] {JobNames.EggCare, JobNames.BabyBeeTutoring}),
                           new Worker("Worker 1", 7.0,
                                      new string[] {JobNames.HiveMaintenance, JobNames.StingPatrol}),
                           new StingPatrol("Sting 1", 7.0,
                                           new string[]
                                               {JobNames.FlowerNectarCollector, JobNames.HoneyManufaturing}),
                           new NectarCollector("Nectar 1", 7.0,
                                               new string[]
                                                   {
                                                       JobNames.FlowerNectarCollector, JobNames.HoneyManufaturing,
                                                       JobNames.EggCare, JobNames.BabyBeeTutoring,
                                                       JobNames.HiveMaintenance
                                                   }),
                           new NectarCollector("Nectart 2", 7.0, JobNames.All),
                       };
        }

        private int GetNumberOfShifts()
        {
            int shifts;

            Console.WriteLine("Enter desired number of shifts for job.");
            Console.Write("Shifts:");
            var input = Console.ReadLine();

            if (!int.TryParse(input, out shifts))
            {
                Console.WriteLine("Please enter a valid number.");
            }

            return shifts;
        }

        private string GetJob()
        {
            int jobNumber;
           
            Console.WriteLine(new string('-', 55));
            Console.WriteLine("Select from the following jobs: \n");

            for (int i = 0; i < JobNames.All.Length; i++)
            {
                Console.WriteLine("\t{0}. {1}", i + 1, JobNames.All[i]);
            }

            Console.Write("\nEnter Job Number: ");
            var input = Console.ReadLine();

            if (!int.TryParse(input, out jobNumber))
            {
                DisplayError("Please enter a valid number");
                return GetJob();
            }

            jobNumber--;

            if(jobNumber < 0  || jobNumber >= JobNames.All.Length)
            {
                DisplayError("Please select a Job Number from the list.");
                return GetJob();
            }

            Console.WriteLine();
            return JobNames.All[jobNumber];
        }

        public void DisplayError(string msg)
        {
            var defaultColor = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Red;

            Console.WriteLine(msg);

            Console.ForegroundColor = defaultColor;
        }
    }
}